package com.jzlin.funhub.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzlin.funhub.admin.common.consts.RedisConst;
import com.jzlin.funhub.admin.model.dto.ArticlePageDTO;
import com.jzlin.funhub.admin.model.req.*;
import com.jzlin.funhub.admin.model.resp.*;
import com.jzlin.funhub.admin.service.article.ArticleStarService;
import com.jzlin.funhub.admin.service.system.SysMessageSessionService;
import com.jzlin.funhub.admin.service.system.SysUserService;
import com.jzlin.funhub.admin.service.system.UserFollowService;
import com.jzlin.funhub.framework.core.http.RestRet;
import com.jzlin.funhub.framework.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 系统管理控制器.
 *
 * @author linjz
 * @since 2025/8/27
 */
@Api(tags ={"系统管理"})
@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private SysMessageSessionService sysMessageSessionService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("查询用户私信会话列表")
    @GetMapping("/message/session/list")
    public RestRet<List<SysMessageSessionResp>> querySessionListByUser(@RequestParam Long userId) {
        return RestRet.createSuccessRet(sysMessageSessionService.querySessionByUser(userId));
    }

    @ApiOperation("查询用户私信会话对话")
    @GetMapping("/message/session/chat")
    public RestRet<SysMessageSessionChatResp> querySessionChat(@RequestParam Long sessionId) {
        return RestRet.createSuccessRet(sysMessageSessionService.querySessionChat(sessionId));
    }

    @ApiOperation("查询用户私信会话对话")
    @GetMapping("/message/session/chat/toUser")
    public RestRet<SysMessageSessionChatResp> querySessionChatByToUser(@RequestParam Long toUserId) {
        return RestRet.createSuccessRet(sysMessageSessionService.querySessionChatByToUser(toUserId));
    }

    @ApiOperation("发送用户私信")
    @PostMapping("/message/post")
    public RestRet<Void> postMessage(@RequestBody SysMessagePostReq req) {
        sysMessageSessionService.postMessage(req);
        return RestRet.createSuccessRet();
    }

    @ApiOperation("获取图形验证码")
    @GetMapping("/captcha")
    @ApiImplicitParams({@ApiImplicitParam(name = "key", value = "验证码生成key")})
    public ResponseEntity captcha(HttpServletResponse response, @RequestParam String captchaKey) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        // 放入redis
        redisUtil.set(RedisConst.CACHE_KEY.CAPTCHA + captchaKey, lineCaptcha.getCode());

        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_PNG)
                .body(lineCaptcha.getImageBytes());
    }
}
